package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.mapper.ShiroUserMapper;
import com.example.demo.pojo.ShiroUser;
import com.example.demo.pojo.User;
import com.example.demo.service.impl.ShiroUserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * shiro controller
 * <p>
 * <p>
 * shiro中的三大对象
 * Subject 用户
 * SecurityManager 管理所有用户
 * Realm 连接数据
 *
 * @author yzh
 * @date 2022/11/22
 */
@Controller
public class IndexController {

    @Autowired
    ShiroUserServiceImpl shiroUserService;
    @Autowired
    ShiroUserMapper shiroUserMapper;

    /**
     * 主页
     *
     * @param model 模型
     * @return {@link String}
     */
    @RequestMapping({"/", "/index"})
    public String index(Model model) {
        model.addAttribute("msg", "Hello Shiro");
        return "/shirouser/index";
    }

    /**
     * add页面
     *
     * @return {@link String}
     */
    @RequestMapping("/user/add")
    public String add() {
        return "/shirouser/add";
    }

    /**
     * update页面
     *
     * @return {@link String}
     */
    @RequestMapping("/user/update")
    public String update() {
        return "/shirouser/update";
    }

    /**
     * 登录页面
     *
     * @return {@link String}
     */
    @RequestMapping("/tologin")
    public String tologin() {
        return "/shirouser/login";
    }

    /**
     * 登录,登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @param model    模型
     * @param session  会话
     * @return {@link String}
     */
    @RequestMapping("login")
    public String login(String username, String password, Model model, HttpSession session) {

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登陆数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        System.out.println(username + password);

        try {
            //执行登陆的方法
            subject.login(token);
            //设置session
            session.setAttribute("loginUser", username);
            model.addAttribute("msg", "欢迎");
            return "/shirouser/index";
        } catch (UnknownAccountException e) {//用户名不存在
            model.addAttribute("msg", "用户名错误");
            return "/shirouser/login";
        } catch (IncorrectCredentialsException e) {//密码不存在
            model.addAttribute("msg", "密码错误");
            return "/shirouser/login";
        }
    }

    @RequestMapping("/toregistered")
    public String toregistered() {
        return "/shirouser/registered";
    }

    /**
     * 注册
     *
     * @param username 用户名
     * @param password 密码
     * @param model    模型
     * @param session  会话
     * @return {@link String}
     */
    @RequestMapping("/Shirouser/registered")

    public String registered(String username, String password, Model model) throws InterruptedException {

        ShiroUser shiroUser = new ShiroUser();
        shiroUser.setName(username);
        shiroUser.setPwd(password);
        shiroUserService.Shiro_registered(shiroUser);
        model.addAttribute("msg", username);
        return "/shirouser/registered";
    }

    /**
     * 测试
     *
     * @param model 模型
     * @return {@link ShiroUser}
     */
    @ResponseBody
    @RequestMapping("/test")
    public ShiroUser test(Model model) {
        ShiroUser shiroUser = shiroUserService.queryUserByName("陈汝旭");
        return shiroUser;
    }

    /**
     * 未授权页面
     *
     * @return {@link String}
     */
    @RequestMapping("/noauth")
    @ResponseBody
    public String noauth() {
        return "未授权页面";
    }
}
